﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace MapEditor
{
    class CopyPaste
    {
        public static CopyPaste Singleton;

        //variables nécéssaire à l'affichage
        private TilesTexture tiles;
        private SpriteBatch spritebatch;

        private Texture2D selectedTexture;
        public List<Vector2I> selected;
        Tile[,] copy;

        public CopyPaste(GraphicsDevice graphicsDevice, TilesTexture tiles, SpriteBatch spritebatch)
        {
            this.tiles = tiles;
            this.spritebatch = spritebatch;
            using (FileStream fs = File.OpenRead("Images/selectedTexture.png"))
                selectedTexture = Texture2D.FromStream(graphicsDevice, fs);
            selected = new List<Vector2I>();
        }

        /// <summary>
        /// dessine la séléction.
        /// </summary>
        /// <param name="posX"> décalage sur X (scrollbar)</param>
        /// <param name="posY"> décalage sur Y (scrollbar)</param>
        public void drawSelection(int y, int x)
        {
                //rectangle définissant la position du curseur sur la grille en fonction de la position de la souris et de la résolution des tiles
                Rectangle r = new Rectangle(0, 0, tiles.Width, tiles.Height);
                spritebatch.Begin();
                foreach (Vector2I v in selected)
                {
                    r.Y = (v.x - x) * tiles.Width;
                    r.X = (v.y - y) * tiles.Height;
                    spritebatch.Draw(selectedTexture, r, new Color(60, 60, 60));
                }
                spritebatch.End();
        }

        /// <summary>
        /// sauvegarde la séléction.
        /// </summary>
        public void saveSelection(Module module)
        {
            int shiftX = getMinX();
            int shiftY = getMinY();
            copy = new Tile[getMaxY() - shiftY + 1, getMaxX() - shiftX + 1];
            foreach (Vector2I v in selected)
            {
                Tile t = module.getTile(v);
                if (t.bg != -2)
                    copy[v.y - shiftY, v.x - shiftX] = t;
            }
        }

        /// <summary>
        /// retourne la posisiton sur X maximum de la séléction.
        /// </summary>
        private int getMaxX()
        {
            int value = int.MinValue;
            foreach (Vector2I v in selected)
            {
                if (value < v.x)
                    value = v.x;
            }
            return value;
        }

        /// <summary>
        /// retourne la posisiton sur Y maximum de la séléction.
        /// </summary>
        private int getMaxY()
        {
            int value = int.MinValue;
            foreach (Vector2I v in selected)
            {
                if (value < v.y)
                    value = v.y;
            }
            return value;
        }

        /// <summary>
        /// retourne la posisiton sur Y minimum de la séléction.
        /// </summary>
        private int getMinY()
        {
            int value = int.MaxValue;
            foreach (Vector2I v in selected)
            {
                if (value > v.y)
                    value = v.y;
            }
            return value;
        }

        /// <summary>
        /// retourne la posisiton sur X minimum de la séléction.
        /// </summary>
        private int getMinX()
        {
            int value = int.MaxValue;
            foreach (Vector2I v in selected)
            {
                if (value > v.x)
                    value = v.x;
            }
            return value;
        }

        /// <summary>
        /// colle la derniére selection enregistré.
        /// </summary>
        /// <param name="module"> module à modifier</param>
        /// <param name="position"> position de collage</param>
        internal void paste(Module module, Vector2I position)
        {
            for (int i = 0; i < copy.GetLength(0); i++)
                for (int j = 0; j < copy.GetLength(1); j++)
                    if (copy[i, j] != null)
                        module.changeTile(position.x + j, position.y + i, copy[i, j]);
        }
    }
}
